package com.elong.qadev.dao.impl;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.elong.qadev.dao.ConfigDaoI;
import com.elong.qadev.entity.ConfigBrowser;
@Repository("configDao")
public class ConfigDaoImpl implements ConfigDaoI {
	Logger logger = Logger.getLogger(ConfigDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public ConfigBrowser getConfigBrowserById(int Id) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				ConfigBrowser.class);
		c.add(Restrictions.eq("id", Id));
		logger.debug("获取的结果是否为空："+c.list().isEmpty());
		if(c.list().isEmpty()){
			return null;
		}
		ConfigBrowser h = (ConfigBrowser) c.list().get(0);
		return h;
	}

	@Override
	public ArrayList<ConfigBrowser> getConfigBrowsers() {
		@SuppressWarnings("unchecked")
		ArrayList<ConfigBrowser> configBrowsers = (ArrayList<ConfigBrowser>) sessionFactory.getCurrentSession().createQuery("from ConfigBrowser").list();
		return configBrowsers;
	}

	@Override
	public boolean saveConfigBrowser(ConfigBrowser configBrowser) {
		sessionFactory.getCurrentSession().save(configBrowser);
		return true;
	}

	@Override
	public boolean deleteConfigBrowser(int id) {
		ConfigBrowser s = getConfigBrowserById(id);
		sessionFactory.getCurrentSession().delete(s);
		return true;
	}

}
